package com.tutu.utils;

import com.tutu.core.Constant;
import com.tutu.core.NoticeMsgType;
import com.tutu.core.api.CommonApiUrls;
import com.tutu.entity.vo.BaseCodeVo;
import com.tutu.entity.vo.BaseMsgVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class BaseUtil {

    /**
     * 获取统一的列表数据
     *
     * @param url    请求地址
     * @param params 请求参数
     * @return 返回结果
     */
    public static List<BaseCodeVo> getListResult(String url, String params) {
        List<BaseCodeVo> list = new ArrayList<>();
        Map<String, Object> map;

        for (Object o : HttpRequestUtil.getListData(url, params)) {
            map = JsonUtil.json2Map(o);

            if (map.containsKey("id")) list.add(new BaseCodeVo(Integer.parseInt(map.get("id") + ""), map.getOrDefault("code", "") + "", map.get("name") + ""));
        }

        return list;
    }

    /**
     * 默认系统正确提示
     */
    public static Map<String, Object> defaultSuccessInfo(Object data) {
        Map<String, Object> map = new HashMap<>();

        // 初始化系统错误提示信息
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", data);

        return map;
    }

    /**
     * 默认系统错误提示
     *
     * @param result 额外提示信息
     * @return 返回结果
     */
    public static Map<String, Object> defaultErrorInfo(String result) {
        Map<String, Object> map = new HashMap<>();

        // 初始化系统错误提示信息
        map.put("code", 1);
        map.put("msg", BaseUtil.isBlank(result) ? CommonApiUrls.DEFAULT_ERROR_INFO : result);
        map.put("data", "");

        return map;
    }

    /**
     * 默认空白的分页信息
     *
     * @return 返回结果
     */
    public static Map<String, Object> defaultPageErrorInfo() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> sort = new HashMap<>();
        Map<String, Object> pageable = new HashMap<>();

        // 初始化空白的分页信息
        sort.put("orders", new ArrayList<>());

        pageable.put("sort", sort);
        pageable.put("page", 0);
        pageable.put("size", Constant.PAGE_DEF_SIZE);

        map.put("total", 0);
        map.put("content", new ArrayList<>());
        map.put("pageable", pageable);

        return map;
    }

    public static List<BaseCodeVo> changeInfo(List<Map<String, Object>> list) {
        List<BaseCodeVo> resultList = new ArrayList<>();

        for (Map<String, Object> map : list)
            resultList.add(new BaseCodeVo(Integer.parseInt(map.get("id") + ""), map.getOrDefault("code", "") + "", map.getOrDefault("name", "") + ""));

        return resultList;
    }

    public static boolean isNotBlank(String str) {
        return StringUtils.isNotBlank(str) && !str.equals("null");
    }

    public static boolean isBlank(String str) {
        return StringUtils.isBlank(str) || str.equals("null");
    }

    // 解析rabbitmq消息
    public static BaseMsgVo getRabbitmqMsg(String content) {
        BaseMsgVo bmv = new BaseMsgVo();

        try {
            String[] str = content.split(Constant.DEFAULT_RABBITMQ_SPLIT);

            bmv.setMsgType(str[0]);

            boolean isNeedChange = str[0].equals(NoticeMsgType.SEND_WECHAT_MESSAGE);

            for (int i = 1; i < str.length; i++)
                bmv.getValueList().add(isNeedChange ? UnicodeUtil.unicodeDecode(str[i]) : str[i]);
        } catch (Exception e) {
            log.info("接收到的Rabbitmq消息有误，" + content);
        }

        return bmv;
    }
}
